package handlers

import (
	"ticket/internal/db"
	"ticket/internal/db/model"
	"ticket/internal/schemas"
	"ticket/pkg/utils"

	"github.com/gin-gonic/gin"
	"github.com/pkg/errors"
)

type ProjectHandler struct{}

func RegisterProjectRouter(group *gin.RouterGroup) {
	service := &ProjectHandler{}
	group.POST("/create", service.Create)
	group.GET("/:id", service.Get)
	group.GET("/list", service.List)
}

// Create godoc
// @Summary 创建项目
// @Description 创建项目
// @Tags 项目
// @ID /project/create
// @Accept json
// @Produce json
// @Param project body schemas.CreateProject true "项目信息"
// @Success 200 {object} schemas.Response{data=model.Project} "项目创建成功"
// @Failure 1004 {object} schemas.Response{error=string} "参数错误"
// @Router /project/create [post]
func (service *ProjectHandler) Create(ctx *gin.Context) {
	params := &schemas.CreateProject{}
	if err := params.BindValidParam(ctx); err != nil {
		schemas.ResponseError(ctx, schemas.ProjectParamInvalid, err)
		return
	}
	user, ok := utils.GetCurrentUser(ctx)
	if !ok {
		schemas.ResponseError(ctx, schemas.ProjectParamInvalid, errors.New("用户不存在"))
		return
	}

	// 如果团队不存在，则不能创建项目
	team := &model.Team{}
	_, err := team.GetByID(db.GormDB, params.TeamID)
	if err != nil {
		schemas.ResponseError(ctx, schemas.ProjectParamInvalid, err)
		return
	}
	project := &model.Project{
		ID:        params.ID,
		Name:      params.Name,
		Introduce: params.Introduce,
		CreateBy:  user.NickName,
		TeamID:    params.TeamID,
	}
	_, err = project.Create(db.GormDB)
	if err != nil {
		schemas.ResponseError(ctx, schemas.ProjectParamInvalid, err)
		return
	}
	schemas.ResponseSuccess(ctx, project)
}

// Get godoc
// @Summary 获取项目
// @Description 获取项目
// @Tags 项目
// @ID /project/:id
// @Accept json
// @Produce json
// @Param id path string true "项目ID"
// @Success 200 {object} schemas.Response{data=model.Project} "项目获取成功"
// @Router /project/:id [get]
func (service *ProjectHandler) Get(ctx *gin.Context) {
	id := ctx.Param("id")
	project := &model.Project{}
	project, err := project.GetByID(db.GormDB, id)
	if err != nil {
		schemas.ResponseError(ctx, schemas.ProjectParamInvalid, err)
		return
	}
	schemas.ResponseSuccess(ctx, project)
}

// List godoc
// @Summary 获取项目列表
// @Description 获取项目列表
// @Tags 项目
// @ID /project/list
// @Accept json
// @Produce json
// @Param team_id query int true "团队ID"
// @Param page query int true "页码"
// @Param pageSize query int true "每页数量"
// @Success 200 {object} schemas.Response{data=[]model.Project} "项目列表获取成功"
// @Router /project/list [get]
func (service *ProjectHandler) List(ctx *gin.Context) {
	params := &schemas.QueryProject{}
	if err := params.BindValidParam(ctx); err != nil {
		schemas.ResponseError(ctx, schemas.ProjectParamInvalid, err)
		return
	}
	project := &model.Project{
		TeamID: params.TeamID,
	}
	projects, err := project.List(db.GormDB, params.Page, params.PageSize)
	if err != nil {
		schemas.ResponseError(ctx, schemas.ProjectParamInvalid, err)
		return
	}
	schemas.ResponseSuccess(ctx, projects)
}
